using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

using System.IO.IsolatedStorage;
using System.Threading;
using System.Drawing;

using UKVSTS;

namespace IEeee
{
    public class IEeeeConfig : AddinConfig
    {
        public String ServerName
        {
            get { return (String) Properties["ServerName"]; }
            set { Properties["ServerName"] = value; }
        }

        public String ProjectName
        {
            get { return (String) Properties["ProjectName"]; }
            set { Properties["ProjectName"] = value; }
        }

        public String WorkItemTypeName
        {
            get { return (String) Properties["WorkItemTypeName"]; }
            set { Properties["WorkItemTypeName"] = value; }
        }

        public String DescriptionFieldName
        {
            get { return (String) Properties["DescriptionFieldName"]; }
            set { Properties["DescriptionFieldName"] = value; }
        }

        public bool IncludeScreenShot
        {
            get { return (bool)Properties["IncludeScreenShot"]; }
            set { Properties["IncludeScreenShot"] = value; }
        }

        public bool IncludeSourceHtml
        {
            get { return (bool)Properties["IncludeSourceHtml"]; }
            set { Properties["IncludeSourceHtml"] = value; }
        }
        public bool IncludeFramesAndIFrames
        {
            get { return (bool)Properties["IncludeFramesAndIFrames"]; }
            set { Properties["IncludeFramesAndIFrames"] = value; }
        }

        public bool IncludeScriptFiles
        {
            get { return (bool)Properties["IncludeScriptFiles"]; }
            set { Properties["IncludeScriptFiles"] = value; }
        }

        public bool IncludeStyleSheets
        {
            get { return (bool)Properties["IncludeStyleSheets"]; }
            set { Properties["IncludeStyleSheets"] = value; }
        }

        public bool IncludeBrowserDetails
        {
            get { return (bool)Properties["IncludeBrowserDetails"]; }
            set { Properties["IncludeBrowserDetails"] = value; }
        }

        public bool ApplyDefaultValues
        {
            get { return (bool)Properties["ApplyDefaultValues"]; }
            set { Properties["ApplyDefaultValues"] = value; }
        }

        public Rectangle ImageEditorScreenCoords
        {
            get { return GetPropAsRectangle ("ImageEditorScreenCoords"); }
            set { Properties["ImageEditorScreenCoords"] = value; }
        }

        private Rectangle GetPropAsRectangle(String index)
        {
            Object savedRect = Properties[index];
            return savedRect == null ? Rectangle.Empty : (Rectangle)savedRect;
        }

        public IEeeeConfig()
            : base(CONFIG_FILENAME)
        {
            if (Load() == false)
            {
                // Set some default values.
                //
                IncludeScreenShot = true;
                IncludeSourceHtml = true;
                IncludeFramesAndIFrames = true;
                IncludeScriptFiles = true;
                IncludeStyleSheets = true;
                IncludeBrowserDetails = true;
                ApplyDefaultValues = true;
            }
        }


        private TeamFoundationServer m_SelectedServer;

        private WorkItemType m_SelectedWorkItemType;

        private Project m_SelectedProject;

        private bool m_DoingAsyncConnect;

        private static IEeeeConfig ms_Instance = new IEeeeConfig ();

        private const String CONFIG_FILENAME = "IEeee.dat";

        
        public static IEeeeConfig Instance
        {
            get { return ms_Instance; }
        }

        public bool IsConnectedToSelectedTFS
        {
            get { return m_SelectedServer != null && m_SelectedProject != null && m_SelectedWorkItemType != null; }
        }

        public bool HasEnoughInfoToConnect {
            get { 
                    return String.IsNullOrEmpty (ServerName) == false &&
                           String.IsNullOrEmpty (ProjectName) == false &&
                           String.IsNullOrEmpty (WorkItemTypeName) == false;
                }
        }

        public void DisconnectFromServer()
        {
            if (m_DoingAsyncConnect == false)
            {
                m_SelectedProject = null;
                m_SelectedWorkItemType = null;

                if (m_SelectedServer != null)
                {
                    m_SelectedServer.Dispose();
                    m_SelectedServer = null;
                }
            }
        }


        delegate bool ConnectToTFSHandler(UICredentialsProvider uiCredProvider);

        public IAsyncResult BeginConnectToSelectedTFS(UICredentialsProvider uiCredProvider, AsyncCallback callback)
        {
            m_DoingAsyncConnect = true;
            ConnectToTFSHandler x = new ConnectToTFSHandler(ConnectToSelectedTFS);
            return x.BeginInvoke(uiCredProvider, callback, x);
        }

        public bool EndConnectToSelectedTFS(IAsyncResult asyncResult)
        {
            ConnectToTFSHandler x = (ConnectToTFSHandler)asyncResult.AsyncState;
            return x.EndInvoke(asyncResult);
        }


        public bool ConnectToSelectedTFS()
        {
            return ConnectToSelectedTFS(null);
        }

        /// <summary>
        /// UICredentialsProvider uiCredProvider = new UICredentialsProvider();
        /// 
        /// </summary>
        /// <param name="uiCredProvider"></param>
        public bool ConnectToSelectedTFS(UICredentialsProvider uiCredProvider)
        {
            bool connected = IsConnectedToSelectedTFS;
            if (connected == false)
            {
                TeamFoundationServer server = null;

                if (uiCredProvider == null)
                {
                    server = TeamFoundationServerFactory.GetServer(ServerName);
                }
                else
                {
                    server = TeamFoundationServerFactory.GetServer(ServerName, uiCredProvider);
                }

                server.Authenticate();

                WorkItemStore store = server.GetService(typeof(WorkItemStore)) as WorkItemStore;

                Project proj = null;
                if (store.Projects.Contains(ProjectName))
                {
                    proj = store.Projects[ProjectName];

                    if (proj.WorkItemTypes.Contains(WorkItemTypeName))
                    {
                        m_SelectedWorkItemType = proj.WorkItemTypes[WorkItemTypeName];
                        m_SelectedProject = proj;
                        m_SelectedServer = server;

                        connected = true;
                    }
                }

                if (server != null && connected == false)
                {
                    server.Dispose();
                }
            }

            m_DoingAsyncConnect = false;
            return connected;
        }

        public TeamFoundationServer SelectedServer
        {
            get { return m_SelectedServer; }
        }

        public Project SelectedProject
        {
            get { return m_SelectedProject; }
        }

        public WorkItemType SelectedWorkItemType
        {
            get { return m_SelectedWorkItemType; }
        }

        public void ConfigureForWiType (WorkItemType wiType)
        {
            ArgTest.IsNotNull (wiType, "wiType");

            m_SelectedWorkItemType = wiType;
            m_SelectedProject = wiType.Project;
            m_SelectedServer = m_SelectedProject.Store.TeamFoundationServer;

            ServerName = m_SelectedServer.Name;
            ProjectName = m_SelectedProject.Name;
            WorkItemTypeName = m_SelectedWorkItemType.Name;

            Save();
        }
    }
}
